﻿using OfficeOpenXml;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

public class ReadExcel : MonoBehaviour
{
    public Dictionary<int, int[]> m_dic_年_年龄人口 = new Dictionary<int, int[]>();
    public 横轴单位 m_横轴单位;
    public List<横轴单位> m_list_横轴单位;
    public int m_n当前年 = 2010;
    public Text m_text;
    public Text m_text0_14;
    public Text m_text15_64;
    public Text m_text65_100;
    public int m_n0_14;
    public int m_n14_64;
    public int m_n65_100;
    public int m_nCount;
    private float[] m_array老龄率 = new float[101];
    private float[] m_array低龄率 = new float[101];
    private int m_nIndex = 0;

    // Start is called before the first frame update
    void Start()
    {
        ReadExcel2();
        for (int i = 0; i < 101; i++)
        {
            GameObject v_objTemp = Instantiate(m_横轴单位.gameObject, new Vector3(0, i, 0), Quaternion.Euler(Vector3.zero));
            横轴单位 v_横轴单位 = v_objTemp.GetComponent<横轴单位>();
            v_横轴单位.SetName(i + "岁");
            m_list_横轴单位.Add(v_横轴单位);
        }
    }

    private void FixedUpdate()
    {
        m_n0_14 = 0;
        m_n14_64 = 0;
        m_n65_100 = 0;
        m_nCount = 0;

        m_text.text = m_n当前年.ToString() + "年";
        for (int i = 0; i < 101; i++)
        {
            if (i < 14)
            {
                m_n0_14 += m_dic_年_年龄人口[m_n当前年][i];
            }
            else
            {
                if (i < 65)
                {
                    m_n14_64 += m_dic_年_年龄人口[m_n当前年][i];
                }
                else
                {
                    m_n65_100 += m_dic_年_年龄人口[m_n当前年][i];
                }
            }
            m_list_横轴单位[i].SetValue(m_dic_年_年龄人口[m_n当前年][i]);
        }
        m_nCount = m_n0_14 + m_n14_64 + m_n65_100;

        m_text0_14.text = "14岁以下" + m_n0_14.ToString() + "人口比重=" + (m_n0_14 * 100.0f / m_nCount) + "%";
        m_text15_64.text = "15岁-64岁" + m_n14_64.ToString() + "人口比重=" + (m_n14_64 * 100.0f / m_nCount) + "%";
        m_text65_100.text = "65岁以上" + m_n65_100.ToString() + "人口比重=" + (m_n65_100 * 100.0f / m_nCount) + "%";

        m_array低龄率[m_nIndex] = m_n0_14 * 100.0f / m_nCount;
        m_array老龄率[m_nIndex] = m_n65_100 * 100.0f / m_nCount;
        m_nIndex++;

        m_n当前年++;
        if (m_n当前年 >= (2010 + 100))
        {
            writeExcel();
#if UNITY_EDITOR
            UnityEditor.EditorApplication.isPlaying = false;
#else
            Application.Quit();
#endif
        }
    }

    private void ReadExcel2()
    {
        FileInfo file = new FileInfo(Application.streamingAssetsPath + "\\hello.xlsx");

        using (var package = new ExcelPackage(file))
        {
            var worksheet = package.Workbook.Worksheets["Sheet1"];


            for (int i = 2010; i < 2110; i++)
            {
                if (!m_dic_年_年龄人口.ContainsKey(i))
                {
                    m_dic_年_年龄人口.Add(i, new int[101]);
                }

                for (int j = 0; j <= 100; j++)
                {
                    m_dic_年_年龄人口[i][j] = int.Parse(worksheet.Cells[j + 2, i - 2009].Value.ToString());
                    Debug.Log(m_dic_年_年龄人口[i][j]);
                }
            }
        }
    }

    private void writeExcel()
    {
        FileInfo file = new FileInfo(Application.streamingAssetsPath + "\\总结.xlsx");

        using (var package = new ExcelPackage(file))
        {
            var worksheet = package.Workbook.Worksheets["Sheet1"];

            int v_nIndex = 1;
            worksheet.Cells[1, 2].Value = "低龄率";
            worksheet.Cells[1, 3].Value = "老龄率";
            for (int i = 0; i < 101; i++)
            {
                worksheet.Cells[i + 2, 1].Value = 2011 + i;
                worksheet.Cells[i + 2, 2].Value = m_array低龄率[i];
                worksheet.Cells[i + 2, 3].Value = m_array老龄率[i];
            }

            package.Save();
        }
    }
}
